package com.luo.controller;

import com.luo.dao.SpecialAccountMapper;
import com.luo.entity.SpecialAccount;
import com.luo.entity.SpecialAccountExample;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2018/3/20.
 */
@Controller
public class test {
    @Autowired
    SpecialAccountMapper specialAccountMapper;

    @RequestMapping("hello")
    @ResponseBody
    public Map hello(){
        Map map = new HashMap();
        map.put("code","ok");
        return map;
    }


    @RequestMapping("hello2")
    @ResponseBody
    public SpecialAccount hello2(){

        return specialAccountMapper.selectByPrimaryKey(1L);
    }


    /**
     * 测试事务异常回滚
     *
     * 在项目中，@Transactional(rollbackFor=Exception.class)，如果类加了这个注解，那么这个类里面的方法抛出异常，就会回滚，数据库里面的数据也会回滚。
     *
     *
     * 学习分布式知识之前计算机一些基础知识要扎实，比如操作系统相关的知识，网络通信相关知识，
     * 软件设计相关知识，然后从了解从了解一些分布式理论知识开始，比如消息中间件，缓存，负载均衡，集群技术，数据同步等，
     * 然后再看分布式开源项目比如 Nginx, Zookeeper，Kafka，Docker，Elastic Search这些，再进阶一点就是看Paper了
     *
     *
     *
     * 事务配置  放 springmvc.xml ???
     * */

    @RequestMapping("hello3")
    @ResponseBody
    public void hello3(){


        SpecialAccountExample example = new SpecialAccountExample();
        SpecialAccountExample.Criteria criteria = example.createCriteria();


         SpecialAccount specialAccount = new SpecialAccount();
         specialAccount.setAccountId(55L);
         specialAccount.setMoney(new BigDecimal(0));
         specialAccountMapper.insertSelective(specialAccount);

         SpecialAccount specialAccount2 = new SpecialAccount();
         specialAccount2.setAccountId(55L);
         specialAccount2.setMoney(new BigDecimal(0));
         specialAccountMapper.insertSelective(specialAccount2);

    }
    @RequestMapping(value = "404")
    public String errors(){
        return "errors/404";
    }

    @RequestMapping(value = "500")
    public String errors500(){
        return "errors/500";
    }


    @RequestMapping(value = "shiroNoAuth")
    public String shiroTest(){
        return "errors/shiroNoAuth";
    }

    @RequestMapping(value = "/vue")
    public String testvue(){
        return "test/testvue";
    }


    @RequestMapping(value = "/sign")
    public String sign(String userName,String password){
        System.out.printf(""+userName);

        UsernamePasswordToken usernamePasswordToken=new UsernamePasswordToken();
        usernamePasswordToken.setUsername(userName);
        usernamePasswordToken.setPassword(password.toCharArray());

        //进入自定义realm  AuthenticationInfo 中验证用户及密码
        SecurityUtils.getSubject().login(usernamePasswordToken);

        //重定向到vue,  进入自定义realm AuthorizationInfo会验证用户是否有权限进入 /vue = authc,prem()
        return "redirect:vue";
    }

}
